/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

using System.Net.Mail;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;

namespace DNNContentBuilder
{
	/// <summary>
	/// Summary description for TaskNotification.
	/// </summary>
	public class TaskNotification
	{
		#region Constants
		public const string resource = "~/DesktopModules/DNNContentBuilder/App_LocalResources/TaskNotification";
		#endregion

		#region Member Variables
		private readonly int workflowId;
		private readonly int metaDataTaskId;
		private readonly int userId;
		private readonly int portalId;
		#endregion

		#region Constructors
		public TaskNotification(int workflowId, int metaDataTaskId, int userId, int portalId)
		{
			this.workflowId		= workflowId;
			this.metaDataTaskId = metaDataTaskId;
			this.userId			= userId;
			this.portalId		= portalId;
		}
		#endregion

		#region Implementation
		public void Send()
		{
			UserInfo user = new UserController().GetUser(portalId, userId);
			PortalInfo portal = new PortalController().GetPortal(portalId);

			using (WorkflowController wrkflowController = new WorkflowController())
			using (MetaDataTaskController mdtController = new MetaDataTaskController())
			{
				WorkflowInfo workflow = wrkflowController.GetItem(workflowId);
				MetaDataTaskInfo task = mdtController.GetItem(metaDataTaskId);
                SmtpClient client = new SmtpClient();
				MailMessage message = new MailMessage();
                MailAddress from = new MailAddress(portal.PortalName);
                client.Host = "localhost";

                message.From = from;
				message.To.Add(user.Membership.Email);
				message.Subject	= GetSubject(workflow, task);
				message.Body	= GetMessageBody(workflow, task);
				
				// Set the message format to HTML
				message.IsBodyHtml = true;

				client.Send(message);
			}
		}

		private string GetSubject(WorkflowInfo workflow, MetaDataTaskInfo task)
		{
			string subject = Commons.GetLocalizedString("subject", resource);
			return MangledString(subject, workflow, task);
		}

		private string GetMessageBody(WorkflowInfo workflow, MetaDataTaskInfo task)
		{
			string message = Commons.GetLocalizedString("message", resource);
			return MangledString(message, workflow, task);
		}

		/// <summary>
		/// Formats the input string replacing occurrences of reserved tokens
		/// with their corresponding values.
		/// </summary>
		/// <param name="input"></param>
		/// <param name="workflow"></param>
		/// <param name="task"></param>
		/// <returns></returns>
		/// <remarks>
		/// The currently supported scan tokens are:
		///		[%Workflow.ID%]
		///		[%Workflow.Title%]
		///		[%Workflow.Description%]
		///		[%Task.ID%]
		///		[%Task.Title%]
		///		[%Task.TaskOrder%]
		///		[%Task.StartDate%]
		/// </remarks>
		private static string MangledString(string input, WorkflowInfo workflow, MetaDataTaskInfo task)
		{
			string result = input;

			result = result.Replace("[%Workflow.ID%]", workflow.WorkflowId.ToString());
			result = result.Replace("[%Workflow.Title%]", workflow.Title);
			result = result.Replace("[%Workflow.Description%]", workflow.Description);
			
			result = result.Replace("[%Task.ID%]", task.MetaDataTaskId.ToString());
			result = result.Replace("[%Task.Title%]", task.Title);
			result = result.Replace("[%Task.TaskOrder%]", task.TaskOrder.ToString());
			result = result.Replace("[%Task.StartDate%]", task.StartDate.ToShortDateString());

			return result;
		}
		#endregion
	}
}
